using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace TrueCode.WPF
{
  /// <summary>
  /// It's inherits <see cref="Freezable"/> in order to inherit data context.
  /// </summary>
  public class ThreadSafeCollectionConverter : Freezable, IValueConverter
  {
    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
      if( ( bool )( DesignerProperties.IsInDesignModeProperty.GetMetadata( typeof( DependencyObject ) ).DefaultValue ) )
        return false;

      return value is IEnumerable
               ? new ThreadSafeCollection( Dispatcher, value )
               : DependencyProperty.UnsetValue;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
      throw new NotSupportedException();
    }

    #region Overrides of Freezable
    protected override Freezable CreateInstanceCore()
    {
      return new ThreadSafeCollectionConverter();
    }
    #endregion
  }
}